﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace WordMine
{
    class Player1
    {
        #region constants
        public int bonusLetters;
        private const int LEVEL_BAR_OFFSET_X = 375;
        private const int LEVEL_BAR_OFFSET_Y = 280;

        //Menu Positions
        private const int SIDEBAR_OFFSET_X = 105;
        private const int SIDEBAR_OFFSET_Y = 240;

        private const int MINER_OFFSET_X = 105;
        private const int MINER_OFFSET_Y = 360;

        private const int BUTTON_BONUS_OFFSET_X = 375;
        private const int BUTTON_BONUS_OFFSET_Y = 40;

        private const int BUTTON_SCORE_OFFSET_X = 375;
        private const int BUTTON_SCORE_OFFSET_Y = 110;

        private const int BUTTON_WORD_OFFSET_X = 375;
        private const int BUTTON_WORD_OFFSET_Y = 180;

        private const int BUTTON_MENU_OFFSET_X = 375;
        private const int BUTTON_MENU_OFFSET_Y = 250;

        //Play Area Positions
        private const int CART_OFFSET_X = 40;
        private const int CART_OFFSET_Y = 40;
        private const int CART_OFFSET_ODD_Y = 40;
        private const int CART_SPACING_X = 50;
        private const int CART_SPACING_Y = 50;

        //Game Parameters
        private const int CART_MIN_ROWS = 7;
        private const int CART_MAX_ROWS = 7;
        private const int CART_COLUMNS = 7;

        private const int CART_SPEED_START_GAME = 150;
        private const int CART_SPEED_PLAYING_GAME = 5;

        private const int ONE_NUGGET_VALUE = 1;
        private const int TWO_NUGGET_VALUE = 2;
        private const int THREE_NUGGET_VALUE = 4;
        #endregion
        //Scoring
        public bool unitCreate;
        private int levelScore;
        public String highestPointWord;
        public int highestPointWordScore;
        public String longestWord;
        public Dictionary<String, int> wordsMade;
        public Rectangle cursorRectangle;
        //Background GameObjects

        private ContentManager loadcontent;
        //Menu GameObjects
        private TokenGameObject buttonBonus;
        private TokenGameObject buttonWord;
        public Cursor cursor;
        //Play Area GameObjects
        public MineCart[,] mineCarts;
        private List<AnimatedInteractableGameObject> explosions;
        private List<MineCart> lastMineCarts;
        KeyboardState current;
        //Game Data
        private Random rand = new Random();
        public int lettersToSend;
        private String language;
        public List<GameObject> gameObjects;
        private Dictionary<String, Double> alphabet;
        private List<String> dictionary;
        private String builtString;
        private int currentX = 0;
        private int currentY = 0;
        private Boolean animating;
        private String mode;
        public List<Unit> Units;
        private MineCart currentMineCart;

        public Player1(String lang, String mode)
        {
            rand = new Random();
            language = lang;
            this.mode = mode;
            this.cursor = Game1.wordMineCursor;
            this.cursorRectangle = new Rectangle(
                (int)cursor.mouse.X,
                (int)cursor.mouse.Y,
                1,
                1
            );
            alphabet = new Dictionary<String, Double>();
            dictionary = new List<String>();
            highestPointWord = "";
            highestPointWordScore = 0;
            longestWord = "";
            wordsMade = new Dictionary<String, int>();
            buttonBonus = new TokenGameObject("menu/buttonJumblev", new Vector2(BUTTON_BONUS_OFFSET_X, BUTTON_BONUS_OFFSET_Y), "");
            buttonWord = new TokenGameObject("menu/buttonWorvd", new Vector2(BUTTON_WORD_OFFSET_X, BUTTON_WORD_OFFSET_Y), "");
            buttonBonus.zindex = 0.3f;
            buttonWord.zindex = 0.3f;
            buttonBonus.fontRotation = -0.1f;
            buttonWord.fontRotation = 0.1f;
            buttonBonus.noAnimate = true;
            buttonWord.noAnimate = true;
            buttonBonus.fontPath = "fonts/WesternSmall";
            buttonWord.fontPath = "fonts/WesternSmall";
            Units = new List<Unit>();
            if (this.language == "numbers")
                buttonBonus.contents = rand.Next(0, 100) + "";
            Start();
        }
        public void Start()
        {
            
            levelScore = 0;
            builtString = "";
            gameObjects = new List<GameObject>();
            //bonusLetters = 0;
            lettersToSend = 0;
           
            unitCreate = false;
           
            gameObjects.Add(buttonBonus);
            gameObjects.Add(buttonWord);


            //Play Area Initialization

            mineCarts = new MineCart[CART_COLUMNS, CART_MAX_ROWS];

            lastMineCarts = new List<MineCart>();



            AnimatedInteractableGameObject explosion;

            explosions = new List<AnimatedInteractableGameObject>();
            for (int i = 0; i < 5; i++)
            {
                explosion = new AnimatedInteractableGameObject("foreground/explosion", new Vector2(0, 0), 25, 5, 10);
                explosion.zindex = 0.4f;
                explosion.finished = true;
                gameObjects.Add(explosion);
                explosions.Add(explosion);
            }
        }
        public void Stop()
        {
            //Play Area GameObjects
            mineCarts = null;
            explosions = null;
            lastMineCarts = null;
            gameObjects = null;
            
        }
        public void Reset()
        {
            Stop();
            Start();
            LoadContent(loadcontent);
        }
        public void LoadContent(ContentManager content)
        {
            loadcontent = content;
            try
            {
                if (mode == "reverse frequency")
                    this.alphabet = content.Load<Dictionary<String, Double>>("data/alphabet_" + this.language + "_reverse");
                else
                    this.alphabet = content.Load<Dictionary<String, Double>>("data/alphabet_" + this.language);
                this.dictionary = content.Load<List<String>>("data/dictionary_" + this.language);
            }
            catch (ContentLoadException e)
            {
                Console.Out.WriteLine("Error: " + e.Message + "\nDefaulting to english");
                this.alphabet = content.Load<Dictionary<String, Double>>("data/alphabet_english");
                this.dictionary = content.Load<List<String>>("data/dictionary_english");
            }
            for (int i = 0; i < CART_COLUMNS; i++)
            {
                for (int j = 0; j < CART_MIN_ROWS; j++)
                {
                    if (j >= CART_MIN_ROWS)
                    {
                        mineCarts[i, j] = null;
                    }
                    else
                    {
                        mineCarts[i, j] = new MineCart();
                    }

                    if (mineCarts[i, j] != null)
                    {
                        mineCarts[i, j].x = i;
                        mineCarts[i, j].y = j;

                        mineCarts[i, j].position.X = CART_OFFSET_X + (i * CART_SPACING_X);
                        mineCarts[i, j].position.Y = -CART_OFFSET_Y + (i * CART_SPACING_Y);

                        mineCarts[i, j].contents = randomLetter().ToUpper();

                        if ((alphabet[mineCarts[i, j].contents.ToLower()] >= 0f) && (alphabet[mineCarts[i, j].contents.ToLower()] < 10f / 14f))
                        {
                            mineCarts[i, j].mineCartType = MineCartType.Gold1;
                        }
                        else if ((alphabet[mineCarts[i, j].contents.ToLower()] >= 10f / 14f) && (alphabet[mineCarts[i, j].contents.ToLower()] < 12f / 14f))
                        {
                            mineCarts[i, j].mineCartType = MineCartType.Gold2;
                        }
                        else if (alphabet[mineCarts[i, j].contents.ToLower()] >= 12f / 14f)
                        {
                            mineCarts[i, j].mineCartType = MineCartType.Gold3;
                        }

                        mineCarts[i, j].zindex = 0.3f;

                        gameObjects.Add(mineCarts[i, j]);
                    }
                }
            }
            foreach (GameObject gameObject in gameObjects)
            {
                gameObject.LoadContent(content);
            }
            currentMineCart = mineCarts[currentX, currentY];
            currentMineCart.tintColor.PackedValue += 150;
        }

        public void Update(int lettersToAdd, GameTime gameTime)
        {
            this.cursorRectangle.X = cursor.mouse.X;
            this.cursorRectangle.Y = cursor.mouse.Y;
            //Selecting Word
            KeyboardState previous = current;
            current = Keyboard.GetState();
            bool breakloop = true;
            for (int x = 0; x < lettersToAdd; x++)
            {
                addMineCart();
            }
            if (current.IsKeyDown(Keys.A) && !previous.IsKeyDown(Keys.A))
            {
                currentX--;
                if (currentX < 0)
                    currentX = CART_COLUMNS - 1;
                while (mineCarts[currentX, currentY] == null)
                {
                    currentX--;
                    if (currentX < 0)
                    {
                        currentX = CART_COLUMNS - 1;
                        breakloop = !breakloop;
                        if (breakloop)
                            break;
                    }

                }
            }
            if (current.IsKeyDown(Keys.D) && !previous.IsKeyDown(Keys.D))
            {
                currentX++;
                if (currentX > CART_COLUMNS - 1)
                    currentX = 0;
                while (mineCarts[currentX, currentY] == null)
                {
                    currentX++;
                    if (currentX > CART_COLUMNS - 1)
                    {
                        currentX = 0;
                        breakloop = !breakloop;
                        if (breakloop)
                            break;
                    }
                }
            }
            if (current.IsKeyDown(Keys.S) && !previous.IsKeyDown(Keys.S))
            {
                currentY--;
                if (currentY < 0)
                    currentY = CART_MAX_ROWS - 1;
                while (mineCarts[currentX, currentY] == null)
                {
                    currentY--;
                    if (currentY < 0)
                    {
                        currentY = CART_MAX_ROWS - 1;
                        breakloop = !breakloop;
                        if (breakloop)
                            break;
                    }
                }
            }
            if (current.IsKeyDown(Keys.W) && !previous.IsKeyDown(Keys.W))
            {
                currentY++;
                if (currentY > CART_MAX_ROWS - 1)
                    currentY = 0;
                while (mineCarts[currentX, currentY] == null)
                {
                    currentY++;
                    if (currentY > CART_MAX_ROWS - 1)
                    {
                        currentY = 0;
                        breakloop = !breakloop;
                        if (breakloop)
                            break;
                    }
                }
            }
            if (current.IsKeyDown(Keys.Space) && !previous.IsKeyDown(Keys.Space))
            {
                wordFinished();
            }
            if (current.IsKeyDown(Keys.E) && !previous.IsKeyDown(Keys.E))
            {
                addLetter(mineCarts[currentX, currentY]);
            }
            if (current.IsKeyDown(Keys.Q) && !previous.IsKeyDown(Keys.Q))
            {
                removeLetter();
            }
            if (cursor.clicking)
            {
                for (int i = 0; i < CART_COLUMNS; i++)
                {
                    for (int j = 0; j < CART_MAX_ROWS; j++)
                    {
                        if (mineCarts[i, j] != null)
                        {
                            if (cursorRectangle.Intersects(mineCarts[i, j].rectangle))
                            {
                                Console.Write(cursorRectangle + " " + mineCarts[i, j].rectangle);
                                buttonWord.fontColor = Color.White;
                                    if (!lastMineCarts.Contains(mineCarts[i, j]))
                                    {
                                        addLetter(mineCarts[i,j]);
                                        break;
                                    }
                                    else
                                    {
                                        removeLetter();
                                        break;
                                    }
                                }
                            }
                    }
                }
            }
            if (currentMineCart != null)
                currentMineCart.tintColor.PackedValue -= 150;
            currentMineCart = mineCarts[currentX, currentY];
            if (currentMineCart == null)
                findAnyValidCart();
            if (currentMineCart != null)
                currentMineCart.tintColor.PackedValue += 150;
            this.animating = false;

            //Reposition carts
            for (int i = 0; i < CART_COLUMNS; i++)
            {
                for (int j = 0; j < CART_MAX_ROWS; j++)
                {
                    //if (lettersToAdd > 0)
                    //{
                    if (mineCarts[i, j] != null)
                    {
                        int mineCartStopPositionY = (Game1.CAMERA_HEIGHT - (mineCarts[i, j].y * CART_SPACING_Y)) - CART_OFFSET_Y;
                        if ((mineCarts[i, j].position.Y + CART_SPEED_PLAYING_GAME) <= mineCartStopPositionY)
                        {
                            this.animating = true;
                            mineCarts[i, j].position.Y += CART_SPEED_PLAYING_GAME;
                        }
                        else
                        {
                            mineCarts[i, j].position.Y = mineCartStopPositionY;
                        }

                        //  }
                    }
                    else { break; }
                }
            }

            if (!this.animating)
            {
                for (int i = 0; i < CART_COLUMNS; i++)
                {
                    if (mineCarts[i, 0] != null)
                    {
                        /*
                        if (mineCarts[i, 0].mineCartType == MineCartType.TNT)
                        {
                            var sortedDict = (from entry in wordsMade orderby entry.Value descending select entry);
                            this.popupLoseScene.fillIn(longestWord, highestPointWord, highestPointWordScore, sortedDict.ElementAt(0).Key, sortedDict.ElementAt(0).Value, wordsMade.Count);
                            this.popupLoseScene.dismissed = false;
                            this.lose = true;
                        }*/
                    }
                }
            }

            /*
            if (levelScore > levelMaxScore)
                levelScore = levelMaxScore;
            if (levelScore < levelMaxScore / 5)
            {
                levelBar.changeBarColor(new Color(0, 255, 0));
            }
            else if (levelScore >= levelMaxScore / 5 && levelScore < levelMaxScore * 2 / 5 && bonusLetters < 1)
            {
                levelBar.changeBarColor(new Color(127, 255, 0));
                makeBonus();
            }
            else if (levelScore >= levelMaxScore * 2 / 5 && levelScore < levelMaxScore * 3 / 5 && bonusLetters < 2)
            {
                levelBar.changeBarColor(new Color(255, 255, 0));
                makeBonus();
            }
            else if (levelScore >= levelMaxScore * 3 / 5 && levelScore < levelMaxScore * 4 / 5 && bonusLetters < 3)
            {
                levelBar.changeBarColor(new Color(255, 127, 0));
                makeBonus();
            }
            else if (levelScore >= levelMaxScore * 4 / 5 && bonusLetters < 4)
            {
                levelBar.changeBarColor(new Color(127, 0, 0));
                makeBonus();
            }
            else if (levelScore == levelMaxScore && bonusLetters < 5)
            {
                makeBonus();

                levelBar.changeBarColor(new Color(255,0, 0));
            }
            levelBar.barFillPercent = ((float)levelScore / (float)levelMaxScore);
            buttonScore.contents = levelScore.ToString();*/
            foreach (GameObject gameObject in gameObjects)
            {
                gameObject.Update(gameTime);
            }
        }
        public void findAnyValidCart()
        {
            foreach (MineCart mineCart in mineCarts)
            {
                if (mineCart != null)
                {
                    currentMineCart = mineCart;
                    currentX = currentMineCart.x;
                    currentY = currentMineCart.y;
                }
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            /* foreach (MineCart minecart in mineCarts)
             {
                 if(minecart!=null)
                     minecart.Draw(spriteBatch);
             }*/
            foreach (GameObject gameObject in gameObjects)
            {
                //  if (gameObject != null)
                gameObject.Draw(spriteBatch);
            }
        }

        private void calculateScore()
        {
            int tempScore = 0;
            for (int i = 0; i < builtString.Length; i++)
            {
                if ((alphabet[builtString[i].ToString().ToLower()] >= 0f) && (alphabet[builtString[i].ToString().ToLower()] < 10f / 14f))
                {
                    tempScore = ONE_NUGGET_VALUE;
                }
                else if ((alphabet[builtString[i].ToString().ToLower()] >= 10f / 14f) && (alphabet[builtString[i].ToString().ToLower()] < 12f / 14f))
                {
                    tempScore = TWO_NUGGET_VALUE;
                }
                else if (alphabet[builtString[i].ToString().ToLower()] >= 12f / 14f)
                {
                    tempScore = THREE_NUGGET_VALUE;
                }
                Units.Add(new Unit(tempScore, 5, new Vector2(50, 100)));
            }
            

            buttonWord.fontColor = Color.Green;

            if (builtString.Length > longestWord.Length)
            {
                longestWord = builtString;
            }
            if (highestPointWordScore < tempScore)
            {
                highestPointWordScore = tempScore;
                highestPointWord = builtString;
            }
            if (wordsMade.ContainsKey(builtString))
            {
                wordsMade[builtString]++;
            }
            else
            {
                wordsMade.Add(builtString, 1);
            }
        }
        public Unit createUnit()
        {
            if(unitCreate)
            {
                unitCreate = false;
                return new Unit(lettersToSend, levelScore, new Vector2(50, 100));
            }
            return null;
        }
        public void makeBonus()
        {

            MineCart mineBonus = null;
            int count = 0;
            for (int i = 0; i < CART_COLUMNS; i++)
            {
                for (int j = 0; j < CART_MAX_ROWS; j++)
                {
                    if (mineCarts[i, j] != null && !mineCarts[i, j].bonus)
                    {
                        count++;
                    }
                }
            }

            while (count != 0)
            {
                mineBonus = mineCarts[rand.Next(0, CART_COLUMNS), rand.Next(0, CART_MAX_ROWS)];
                if (mineBonus != null && !mineBonus.bonus)
                {
                    mineBonus.bonus = true;
                    mineBonus.tintColor = Color.Green;
                    bonusLetters++;
                    return;
                }
            }
        }
        private String randomLetter()
        {
            String randomLetter = "e";

            double randomNumber = rand.NextDouble();


            randomNumber = rand.NextDouble();

            foreach (KeyValuePair<String, Double> letter in alphabet)
            {
                if (randomNumber <= letter.Value)
                {
                    randomLetter = letter.Key;
                    break;
                }
            }

            return randomLetter;
        }

        public void randomizeCart(MineCart mineCart)
        {
            mineCart.fontColor = Color.White;
            mineCart.contents = randomLetter().ToUpper();

            if ((alphabet[mineCart.contents.ToLower()] >= 0f) && (alphabet[mineCart.contents.ToLower()] < 10f / 14f))
            {
                mineCart.mineCartType = MineCartType.Gold1;
            }
            else if ((alphabet[mineCart.contents.ToLower()] >= 10f / 14f) && (alphabet[mineCart.contents.ToLower()] < 12f / 14f))
            {
                mineCart.mineCartType = MineCartType.Gold2;
            }
            else if (alphabet[mineCart.contents.ToLower()] >= 12f / 14f)
            {
                mineCart.mineCartType = MineCartType.Gold3;
            }
            mineCart.deselected();
        }


        public void removeMineCart(MineCart temp)
        {


            gameObjects.Remove(mineCarts[temp.x, temp.y]);
            mineCarts[temp.x, temp.y] = null;
            for (int k = temp.y + 1; k < CART_MAX_ROWS; k++)
            {
                if (mineCarts[temp.x, k] != null)
                {
                    mineCarts[temp.x, k - 1] = mineCarts[temp.x, k];
                    mineCarts[temp.x, k - 1].fontColor = Color.White;
                    mineCarts[temp.x, k - 1].x = temp.x;
                    mineCarts[temp.x, k - 1].y = k - 1;
                    mineCarts[temp.x, k] = null;
                }
            }
            /*
            temp.position.X = CART_OFFSET_X + (temp.x * CART_SPACING_X);
            temp.position.Y = -CART_OFFSET_Y - ((CART_MAX_ROWS - temp.y) * CART_SPACING_Y);

            temp.y = CART_MAX_ROWS - 1;

           // randomizeCart(temp);

           // mineCarts[temp.x, temp.y] = temp;*/
        }
        public void addMineCart()
        {

            MineCart temp = new MineCart();
            temp.LoadContent(loadcontent);

            for (int y = 0; y < CART_MAX_ROWS; y++)
            {
                for (int x = 0; x < CART_COLUMNS; x++)
                {

                    if (mineCarts[x, y] == null)
                    {
                        temp.x = x;
                        temp.y = y;

                        temp.position.X = CART_OFFSET_X + (x * CART_SPACING_X);
                        temp.position.Y = -((CART_MAX_ROWS) * CART_SPACING_Y) + ((CART_MAX_ROWS - y) * CART_SPACING_Y);

                        randomizeCart(temp);

                        mineCarts[x, y] = temp;
                        gameObjects.Add(mineCarts[x, y]);
                        for (int k = CART_MAX_ROWS - 1; k >= temp.y + 1; k--)
                        {
                            if (mineCarts[temp.x, k] != null)
                            {
                                mineCarts[temp.x, k - 1] = mineCarts[temp.x, k];
                                mineCarts[temp.x, k - 1].fontColor = Color.White;
                                mineCarts[temp.x, k - 1].x = temp.x;
                                mineCarts[temp.x, k - 1].y = k - 1;
                                mineCarts[temp.x, k] = null;
                            }
                        }
                        return;
                    }
                }
            }
           
        }

        public void addLetter(MineCart addCart)
        {
            if (!lastMineCarts.Contains(addCart) && addCart != null)
            {
                addCart.fontColor = Color.Gold;
                addCart.selected();
                lastMineCarts.Add(addCart);
                builtString += addCart.contents.ToLower();
                buttonWord.contents = builtString.ToUpper();
            }
        }
        public void removeLetter()
        {
            if (lastMineCarts.Count > 0)
            {
                lastMineCarts[lastMineCarts.Count - 1].fontColor = Color.White;
                lastMineCarts[lastMineCarts.Count - 1].deselected();
                builtString = builtString.Substring(0, builtString.Length - 1);
                buttonWord.contents = builtString;
                lastMineCarts.Remove(lastMineCarts[lastMineCarts.Count - 1]);
            }
        }

        public int sendLetters()
        {
            if (mode == "endless")
            {
                if (lettersToSend > 1)
                    lettersToSend = 1;
            }
            int templetters = lettersToSend;
            lettersToSend = 0;
            return templetters;
        }
        public void wordFinished()
        {
            Boolean pass = false;
            if (builtString != null)
            {
                if (this.language == "numbers")
                {
                    pass = testEquation();
                    if (pass)
                        buttonBonus.contents = rand.Next(0, 100) + "";
                }
                if(mode != "bigger is better" || builtString.Length >=5)
                {


                    if (dictionary.Contains(builtString.ToLower()) || pass)
                    {
                        calculateScore();
                        unitCreate = true;
                        lettersToSend += builtString.Length;
                        if (mode == "double or nothing")
                        {
                            if (builtString.Length >= 5)
                                lettersToSend *= 2;
                            else
                                lettersToSend -= builtString.Length;
                        }
                        lastMineCarts.Sort();
                        for (int i = 0; i < lastMineCarts.Count; i++)
                        {
                            if (lastMineCarts[i].bonus)
                            {
                                recursiveGetAdjacent(lastMineCarts[i]);
                                bonusLetters--;
                                levelScore -= 200;
                            }
                            removeMineCart(lastMineCarts[i]);
                        }
                        switch (builtString.Length)
                        {
                            case 1:
                            case 2:
                            case 3:
                                break;
                            case 4:

                                break;
                            case 5:

                                break;
                            case 6:

                                break;
                            default:
                                break;
                        }
                        builtString = null;
                        lastMineCarts.Clear();
                    }
                    else
                    {
                        buttonWord.fontColor = Color.Red;
                    }
                }
            }
        }
        public void recursiveGetAdjacent(MineCart lookup)
        {
            foreach (MineCart adjacent in getAdjacentMineCarts(lookup))
            {
                if (!lastMineCarts.Contains(adjacent))
                {
                    lastMineCarts.Add(adjacent);
                }/*
                if (!lastMineCarts.Contains(adjacent) && adjacent.bonus)
                {
                    lastMineCarts.Add(adjacent);
                    recursiveGetAdjacent(adjacent);
                }
                */
            }
        }
        public Boolean testEquation()
        {
            int number = 0;
            int prevNumber = 0;
            int numberOfSymbols = 0;
            int currNumber = 0;
            char currSymbol = 'a';
            bool previousWasSymbol = false;
            for (int i = 0; i < builtString.Length; i++)
            {
                char builtChar = Convert.ToChar(builtString.Substring(i, 1));
                if (new[] { '+', '-', '/', '*' }.Contains(builtChar))
                {
                    if (previousWasSymbol || (i == 0) || (i == builtString.Length - 1))
                    {
                        return false;
                    }
                    numberOfSymbols++;
                    if (numberOfSymbols == 1)
                    {
                        number = prevNumber;
                    }
                    else
                    {
                        for (int x = 1; x <= prevNumber.ToString().Length - 1; x++)
                        {
                            numberOfSymbols += x;
                        }
                        switch (currSymbol)
                        {
                            case '+':
                                number += prevNumber;
                                break;
                            case '-':
                                number -= prevNumber;
                                break;
                            case '*':
                                number *= prevNumber;
                                break;
                            case '/':
                                number /= prevNumber;
                                break;
                            default:
                                break;
                        }
                    }
                    currSymbol = builtChar;
                    prevNumber = 0;
                    currNumber = 0;
                    previousWasSymbol = true;
                }
                else
                {
                    currNumber = int.Parse(builtChar.ToString());
                    previousWasSymbol = false;
                    if (prevNumber > 0)
                    {
                        currNumber += prevNumber * 10;
                    }
                }
                prevNumber = currNumber;
            }
            for (int x = 1; x <= prevNumber.ToString().Length - 1; x++)
            {
                numberOfSymbols += x;
            }
            switch (currSymbol)
            {
                case '+':
                    number += prevNumber;
                    break;
                case '-':
                    number -= prevNumber;
                    break;
                case '*':
                    number *= prevNumber;
                    break;
                case '/':
                    number /= prevNumber;
                    break;
                default:
                    number = prevNumber;
                    break;
            }
            Console.Out.WriteLine("Final: " + number + "Number of Symbols: " + numberOfSymbols);
            if (number == int.Parse(buttonBonus.contents))
                return true;
            return false;
        }
        private List<MineCart> getAdjacentMineCarts(MineCart currentMineCart)
        {
            List<MineCart> adjacentMineCarts = new List<MineCart>();

            if (currentMineCart.y > 0)
            {
                if (mineCarts[currentMineCart.x, currentMineCart.y - 1] != null)
                {
                    adjacentMineCarts.Add(mineCarts[currentMineCart.x, currentMineCart.y - 1]);
                }
                if (currentMineCart.x > 0)
                {
                    if (mineCarts[currentMineCart.x - 1, currentMineCart.y - 1] != null)
                    {
                        adjacentMineCarts.Add(mineCarts[currentMineCart.x - 1, currentMineCart.y - 1]);
                    }
                }
                if (currentMineCart.x < CART_COLUMNS - 1)
                {
                    if (mineCarts[currentMineCart.x + 1, currentMineCart.y - 1] != null)
                    {
                        adjacentMineCarts.Add(mineCarts[currentMineCart.x + 1, currentMineCart.y - 1]);
                    }
                }
            }
            if (currentMineCart.y < CART_MAX_ROWS - 1)
            {
                if (mineCarts[currentMineCart.x, currentMineCart.y + 1] != null)
                {
                    adjacentMineCarts.Add(mineCarts[currentMineCart.x, currentMineCart.y + 1]);
                }
                if (currentMineCart.x > 0)
                {
                    if (mineCarts[currentMineCart.x - 1, currentMineCart.y + 1] != null)
                    {
                        adjacentMineCarts.Add(mineCarts[currentMineCart.x - 1, currentMineCart.y + 1]);
                    }
                }
                if (currentMineCart.x < CART_COLUMNS - 1)
                {
                    if (mineCarts[currentMineCart.x + 1, currentMineCart.y + 1] != null)
                    {
                        adjacentMineCarts.Add(mineCarts[currentMineCart.x + 1, currentMineCart.y + 1]);
                    }
                }
            }

            if (currentMineCart.x > 0)
            {
                if (mineCarts[currentMineCart.x - 1, currentMineCart.y] != null)
                {
                    adjacentMineCarts.Add(mineCarts[currentMineCart.x - 1, currentMineCart.y]);
                }
            }

            if (currentMineCart.x < CART_COLUMNS - 1)
            {
                if (mineCarts[currentMineCart.x + 1, currentMineCart.y] != null)
                {
                    adjacentMineCarts.Add(mineCarts[currentMineCart.x + 1, currentMineCart.y]);
                }
            }

            return adjacentMineCarts;
        }
    }
}
/* if (cursor.clicking)
                           {
                               for (int i = 0; i < CART_COLUMNS; i++)
                               {
                                   for (int j = 0; j < CART_MAX_ROWS; j++)
                                   {
                                       if (mineCarts[i, j] != null)
                                       {
                                           if (cursorRectangle.Intersects(mineCarts[i, j].rectangle))
                                           {
                                               buttonWord.fontColor = Color.White;

                                                   if (!lastMineCarts.Contains(mineCarts[i, j]))
                                                   {

                                                       mineCarts[i, j].finished = false;
                                                       foreach (MineCart m in getAdjacentMineCarts(mineCarts[i, j]))
                                                       {
                                                           if (m.fontColor != Color.Gold)
                                                           {
                                                               m.finished = false;
                                                           }
                                                       }
                                                       mineCarts[i, j].fontColor = Color.Gold;
                                                       lastMineCarts.Add(mineCarts[i, j]);
                                                       builtString += mineCarts[i, j].contents.ToLower();
                                                       buttonWord.contents = builtString.ToUpper();

                                                   }
                                                   else if (lastMineCarts.Count > 1)
                                                   {
                                                       //ArgumentOutOfRange Exception
                                                       if (mineCarts[i, j] == lastMineCarts[lastMineCarts.Count - 2])
                                                       {
                                                           lastMineCarts[lastMineCarts.Count - 1].fontColor = Color.White;
                                                           builtString = builtString.Substring(0, builtString.Length - 1);
                                                           buttonWord.contents = builtString;
                                                           lastMineCarts.Remove(lastMineCarts[lastMineCarts.Count - 1]);
                                                       }
                                                   }
                                               
                                           }
                                       }
                                   }
                               }
                           }*/